package cn.iocoder.yudao.hires.service.notice.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.hires.entity.NoticeOfferDO;
import cn.iocoder.yudao.hires.mapper.NoticeApplicationMapper;
import cn.iocoder.yudao.hires.mapper.NoticeEnterpriseMapper;
import cn.iocoder.yudao.hires.mapper.NoticeOfferMapper;
import cn.iocoder.yudao.hires.service.notice.NoticeOfferService;
import cn.iocoder.yudao.hires.vo.PostTionShortVo;
import cn.iocoder.yudao.hires.vo.noticeoffer.NoticeOfferPageReqVO;
import cn.iocoder.yudao.hires.vo.noticeoffer.NoticeOfferSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingyuv.captcha.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.NOT_EXISTS;

/**
 * 下游-Offer通知 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class NoticeOfferServiceImpl implements NoticeOfferService {

    @Resource
    private NoticeOfferMapper noticeOfferMapper;
    @Resource
    private NoticeApplicationMapper noticeApplicationMapper;
    @Override
    public Long createNoticeOffer(NoticeOfferSaveReqVO createReqVO) {
        // 插入
        NoticeOfferDO noticeOffer = BeanUtils.toBean(createReqVO, NoticeOfferDO.class);
        PostTionShortVo category = noticeApplicationMapper.getCategory(noticeOffer.getPositionId());
        noticeOffer.setPositionCategoryId(category.getCategoryId());
        noticeOffer.setPositionCategoryName(category.getCategoryName());
        noticeOfferMapper.insert(noticeOffer);
        // 返回
        return noticeOffer.getId();
    }

    @Override
    public void updateNoticeOffer(NoticeOfferSaveReqVO updateReqVO) {
        // 校验存在
        validateNoticeOfferExists(updateReqVO.getId());
        // 更新
        NoticeOfferDO updateObj = BeanUtils.toBean(updateReqVO, NoticeOfferDO.class);
        noticeOfferMapper.updateById(updateObj);
    }

    @Override
    public void deleteNoticeOffer(Long id) {
        // 校验存在
        validateNoticeOfferExists(id);
        // 删除
        noticeOfferMapper.deleteById(id);
    }

    private void validateNoticeOfferExists(Long id) {
        if (noticeOfferMapper.selectById(id) == null) {
            throw exception(NOT_EXISTS);
        }
    }

    @Override
    public NoticeOfferDO getNoticeOffer(Long id) {
        return noticeOfferMapper.selectById(id);
    }

    @Override
    public PageResult<NoticeOfferDO> getNoticeOfferPage(NoticeOfferPageReqVO pageReqVO) {
        return noticeOfferMapper.selectPage(pageReqVO);
    }

    @Override
    public List<NoticeOfferDO> getNoticeOfferList(Long userId,String tenantName, Long tenantId,Integer status) {
        return noticeOfferMapper.getNoticeOfferList(userId,tenantName,tenantId,status);
    }

}